package com.myfoon.view.application;


import com.myfoon.view.context.helper.BeanReceiver;

import javax.faces.context.FacesContext;
import javax.faces.event.PhaseEvent;
import javax.faces.event.PhaseId;
import javax.faces.event.PhaseListener;


public class MFPhaseListener implements PhaseListener {
    public MFPhaseListener() {
    }

    public void afterPhase(PhaseEvent phaseEvent) {
        FacesContext context = null;
        
        if (phaseEvent != null) {
        context = phaseEvent.getFacesContext();                        
        }
        
        if (context != null ) {
        LocaleBean localeBean = (LocaleBean)BeanReceiver.receiveBean("locales");                
        context.getViewRoot().setLocale(localeBean.getCurrentLocale());        
        }
        /*
        System.out.println(context.getViewRoot().getViewId());
        LoginBean bean = (LoginBean)BeanReceiver.receiveBean("loginBean");        
        String viewId = context.getViewRoot().getViewId();
                
        NavigationHandler nh = context.getApplication().getNavigationHandler();
        
        if (bean.isLoggedIn()) {
            if (viewId.lastIndexOf("signup.xhtml") > -1) {
                nh.handleNavigation(context, null, "already_logged_in");        
            }
        }
        */
        /*       
        Map<String, String> parameters =
            context.getExternalContext().getRequestParameterMap();
        
        String category = null;
        
        if (parameters != null) {        
            category =
                    parameters.get("category");
        }                
        
        if (category != null && category.length() > 0) {
            Application application = context.getApplication();

            application.getNavigationHandler();
        }        
        */
        /*
        boolean loginPage = context.getViewRoot().getViewId().lastIndexOf("login") > -1 ? true : false;
        ELContext elContext = FacesContext.getCurrentInstance().getELContext();
        LoginBacking loginBacking
            = (LoginBacking) FacesContext.getCurrentInstance().getApplication()
                .getELResolver().getValue(elContext, null, "LoginBacking");

        if (!loginPage && !loginBacking.getLoggedIn()) {
            NavigationHandler nh = context.getApplication().getNavigationHandler();
            nh.handleNavigation(context, null, "authentication_failure");
        }
        */
        return;
    }

    public void beforePhase(PhaseEvent phaseEvent) {
    }

    public PhaseId getPhaseId() {
        return PhaseId.RESTORE_VIEW;
    }
}
